import socket
import web_IDS
import pcap_IDS
import struct
import Linux_IDS
import Windows_IDS
import Linux_rdp_IDS
import time

def Server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server.bind(("localhost",6969))  #绑定监听端口

    server.listen(5)  #监听

    print("[*]Server正在监听......")

    while True:
        conn,addr = server.accept() #等待连接
        print("[+]服务端已连接......")
        print("[+]conn:",conn,"\naddr:",addr)
        while True:
            agent_response = conn.recv(1024)  # 接受消息的大小
            first_size = int(agent_response.decode("utf-8"))
            conn.send("-------------------------------------".encode("utf-8"))
            agent_response = conn.recv(first_size)  # 接收数据大小
            file_size = int(agent_response.decode("utf-8"))
            print("接收到的大小：", file_size)
            conn.send(
                "确认接收----------------------------------------------------------------------------------------------------------------------------------------".encode(
                    "utf-8"))  # 确认接收1

            agent_response = conn.recv(1024)
            second_size = int(agent_response.decode("utf-8"))
            conn.send("-------------------------------------".encode("utf-8"))
            agent_response = conn.recv(second_size)  # 接收文件类型
            log_type = str(agent_response.decode("utf-8"))
            print(log_type)
            conn.send("---------------------------------".encode("utf-8"))  # 确认接收2

            agent_response = conn.recv(1024)  # 接收filename
            thrird_size = int(agent_response.decode("utf-8"))
            conn.send("--------------------------------".encode("utf-8"))
            agent_response = conn.recv(thrird_size)
            file_name = str(agent_response.decode("utf-8"))
            print(file_name)

            if log_type == "Linuxcmd":
                if "rdp" in file_name:
                    Linux_rdp_IDS.Linux_rdp_IDS(conn, file_name, file_size)
                else:
                    Linux_IDS.Linux_IDS(conn, file_name, file_size)
            if log_type == "Windowscmd":
                Windows_IDS.Windows_IDS(conn, file_name, file_size)
            if log_type == "Webcmd":
                web_IDS.web_IDS(conn, file_name, file_size)
            if log_type == "Pcapcmd":           #send-send
                pcap_IDS.pcap_IDS(conn, file_name, file_size)



        conn.close()
Server()






